import { moduleChange, commonDict } from "/@/api/index"
import { Toast } from '/@/utils/Message';
import { sm2 } from "sm-crypto"
/**
     * @description 用户行为
     * @param{String}uuid 后台接口返回的uuid
     * @param{String}entryOrExit 1进入模块 2 退出模块
     * @param{String}modelName 模块名字
     * @param callback 回调函数
     **/
export function recordUserBehavior(uuid:any, entryOrExit:any, modelName:any, callback:any) {
    const currentTime = getLocalTime()
    const data = {
        id: uuid,
        userId:"",
        type:"",
        modelEntryTime:'',
        modelExitTime:'',
        modelName:modelName,
        phoneSystem:""
    }
    if(entryOrExit == '1') {
        data.modelEntryTime = currentTime
        data.modelExitTime = ''
    }else{
        data.modelEntryTime = ''
        data.modelExitTime = currentTime
    }
    // moduleChange(data).then((res:any) => {
    //     if(res.code == 200) {
    //         const uuid = res.data.id
    //         if(uuid) {
    //             callback(uuid)
    //         }
    //     }
       
    // })

}
function getLocalTime() {
    const d = new Date() // 根据时间戳生成的时间对象
    const date = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`
    return date
}

// 是否显示导航栏和状态栏
export const isShowStatusAndNavigator = ():number => {
    if(localStorage.getItem('statusBar') == 'true' && localStorage.getItem('navigator') == 'true') {
        return 2
    }else if(localStorage.getItem('statusBar') == 'true' || localStorage.getItem('navigator') == 'true') {
        return 1
    }else {
        return 0
    }
}

// 获取身份证类型
export const getIdType = (idType:any):Promise<string> => {
    return new Promise((resolve) => {
        commonDict("certi_type").then((res:any) => {
            if(res.code == 200) {   
                const codeName = res.data.find((item:any) => item.codeCode == idType)?.codeName
                resolve(codeName)
            }else {
                Toast("证件类型获取失败")
            }
        })
    })
}

// sm2 加密
export const sm2Encryption = async(key, data) => {  
    const publicKey = key
    const cipherMode = 1
    const result = sm2.doEncrypt(data, publicKey, cipherMode)
    return `04${result}`

}

/**  
 * 节流函数类型定义  
 * @param func 需要节流的函数  
 * @param wait 节流时间间隔，单位为毫秒  
 * @returns 节流后的函数  
 */  
export function throttle<T extends (...args: any[]) => void>(  
    func: T,  
    wait: number  
  ): (...args: Parameters<T>) => void {  
      
      let lastTime: number = 0;  
      
      return function(this: any, ...args: Parameters<T>) {  
          const now = Date.now();  
          
          if (now - lastTime >= wait) {  
        lastTime = now;  
        func.apply(this, args);  
      }  
    };  
  } 